Task References

On this page:

search (string)

Purpose

The search task takes a regular expression (RegExp) and a string and searches for that RegExp within the string. A RegExp can contain both special and ordinary characters. The task returns an index (number) to indicate the position where a match is found. Indexing starts from zero (0). The task returns -1 if no match is found.

Potential Use Case

The search task checks if a particular string matches for a given regex, or search pattern. You could use this method simply to check if a certain pattern exists and also know its index within a string. In conjunction with the substr method, you could use search to find and replace text in a data record.

Properties

Input and output properties are shown below.

Input Type Description
str String Required. The string (or substring) to search.
regexp String Required. The search pattern to match in the regular expression.


Output Type Description
index Number The position where the first match between the regular expression and the given string is found. Returns -1 if not found. Of note, the outgoing index of the first character begins with 0, the second character 1, and so on.

Examples

Example 1

In the IAP examples shown below:

  • The incoming str variable is statically set. The reference variable is Hello World.

  • The search pattern to match in the regexp variable is or.

    search-string

  • The index result will return 7 upon output, indicating the regexp ("or") is located at index position 7 in the string.

    search-string

Example 2

In the IAP examples shown below:

  • The reference variable for the incoming str is Hey Dr. Livingstone.

  • The search pattern to match in the regexp variable is \., which is a special notation (character class) to indicate the match should be a period (".").

    Refer to this resource for more information on using character classes in regular expressions.

    search-string

  • The index result returns 6 upon output, indicating the period is located at index position 6 in the string.

    search-string

Example 3

In the IAP examples shown below:

  • The incoming str variable is statically set. The reference variable is Hey Judey.

  • The regexp variable is statically set as \., a character class notation to indicate the match should be a period (".").

    search-string

  • The index result returns -1 upon output, indicating a period was not found in the string.

    search-string